<?php

namespace app\command;

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Sms\Sms;
use app\common\facade\Arr;
use app\common\facade\Help;
use app\common\services\AliyunSmsServicer;
use app\common\services\JobService;
use app\common\services\PublishJobService;
use app\common\services\wechattemplate\JobTemplateService;
use app\common\services\WechatTemplateService;
use app\common\validate\MarketGoodsValidate;
use app\wechat\service\WechatService;
use Carbon\Carbon;
use library\tools\Data;
use think\console\Input;
use think\console\Output;
use think\Db;
use think\facade\Config;

class TestCommand extends BasicCommand
{

    protected $name = 'test';

    protected $desc = '测试命令程序';


    protected function execute(Input $input, Output $output)
    {


        $this->__fixBugPublishJobTemplate();


    }


    /**
     * @throws \Exception
     */
    private function __fixBugPublishJobTemplate()
    {
        $postData = [
            "extendId" => 0,
            "id" => 0,
            "work_type" => 6,
            "contacts" =>  "王二狗",
            "mobile" =>  "18468015973",
            "title" =>  "范美希测试",
            "work_time" => 1586102400,
            "work_time_end" => 1586102400,
            "work_up_time" =>  "08:30",
            "work_down_time" =>  "15:30",
            "address" => "南白象大街",
            "provinces_name" => " 浙江省 温州市 瓯海区",
            "salary" =>  "100",
            "salary_unit" => 3,
            "need_worker" => "8",
            "settlement_type" => 1,
            "gender" => 2,
            "content" => "礼仪接待",
            "requirement" =>  "长相甜美端庄大方",
            "work_welfare" =>  "",
            "user_id" => 1,
            "business_id" => 0,
            "status" => 1,
            "admin_id" => 0,
        ];

        try {
            $result = (new PublishJobService())->publishJob($postData);
        } catch (\Exception $e) {
            dump('command output');
            halt($e->getMessage());
        }
        halt($result);


    }
























    private function __testSms()
    {
        try {
            $aa = (new AliyunSmsServicer())->sendSmsCode('15912063113', '451006');
            var_dump($aa);exit;
        } catch (\Exception $e) {
            halt($e->getMessage());
        }

    }


    /**
     * @throws \AlibabaCloud\Client\Exception\ClientException
     * @throws \AlibabaCloud\Client\Exception\ServerException
     */
    private function __sendSms()
    {



        AlibabaCloud::accessKeyClient('LTAI4FkkCgMbg9UVUfZBUdYQ', 'hTAEpR0IYiMtZgWR1Xg2CjNGoxtsHo')
            ->regionId('cn-hangzhou')
            ->asDefaultClient();

        try {
            $templateParam = ['code' => 545666];
            $result = AlibabaCloud::rpc()
                ->product('Dysmsapi')
                ->version('2017-05-25')
                ->action('SendSms')
                ->method('POST')
                ->host('dysmsapi.aliyuncs.com')
                ->options([
                    'query' => [
                        'RegionId' => "cn-hangzhou",
                        'PhoneNumbers' => "18468015963",
                        'SignName' => "掌上汇同城",
                        'TemplateCode' => "SMS_181867468",
                        'TemplateParam' => json_encode($templateParam),
                    ],
                ])
                ->request();
            print_r($result->toArray());
        } catch (ClientException $e) {
            echo $e->getErrorMessage() . PHP_EOL;
        } catch (ServerException $e) {
            echo $e->getErrorMessage() . PHP_EOL;
        }




        exit();


    }



    private function __addMajor()
    {
        $content = file_get_contents('/Users/feraner/phpWorker/partTimeJob/major.txt');

        $list = explode(PHP_EOL, $content);

        foreach ($list as $item) {
            if ($item) {
                $itemArr = json_decode($item, true);
                foreach ($itemArr as $one) {
                    if (Arr::get($one, 'majorName')) {
                        Db::name('school_major')
                            ->insertGetId([
                                'name'      => Arr::get($one, 'majorName', ''),
                                'short_name'      => Arr::get($one, 'majorName', ''),
                                'created_at'    => time(),
                                'updated_at'        => time()
                            ]);
                    }
                }
            }
        }

        halt('done');
    }




    private function __addSchoole()
    {
        $content = file_get_contents('/Users/feraner/phpWorker/partTimeJob/school.txt');
        $list = explode(PHP_EOL, $content);
        $num = 1;
        foreach ($list as $item) {
            if ($item) {
                try {
                    $tmpItem = explode('|||', $item);
                    $img = file_get_contents($tmpItem[0]);
                    file_put_contents('/Users/feraner/phpWorker/partTimeJob/public/upload/school/' . $num . '.jpg', $img);
                    Db::name('school')
                        ->insertGetId([
                            'name'      => $tmpItem[1],
                            'short_name'=> $tmpItem[1],
                            'icon'      => '/upload/school/' . $num . '.jpg',
                            'created_at' => time(),
                            'updated_at' => time()
                        ]);
                } catch (\Exception $e) {
                    Db::name('school')
                        ->insertGetId([
                            'name'      => $tmpItem[1],
                            'short_name'=> $tmpItem[1],
                            'icon'      => '/static/front/images/icon/user-setting-school.png',
                            'created_at' => time(),
                            'updated_at' => time()
                        ]);
                }
                $num +=1;
            }
        }

        halt('done');

    }




    private function __areaHandle()
    {

        $content = file_get_contents('http://testjob.com/static/front/plugs/vant/json/1.txt');

        $contentArr = explode(PHP_EOL, $content);

        foreach ($contentArr as $item) {
            if ($item) {
                $tmpItem = explode(':', $item);
                $tmpCode = trim(Arr::get($tmpItem, 0));
                $tmpName = str_replace(
                    ['市', '省', '自治区','特别行政区','自治州','地区','县','岛','区','自治县','旗'],
                    '',
                    trim(Arr::get($tmpItem, 1)));

                $areaInfo = Db::name('area')
                    ->where('area_name', 'LIKE', '%' . $tmpName . '%')
                    ->find() ?:[];
                if ($areaInfo) {
                    Db::name('area')
                        ->where('area_id', Arr::get($areaInfo, 'area_id'))
                        ->where('area_code', '')
                        ->update([
                            'area_code'     => $tmpCode,
                            'area_deep'     => 2
                        ]);
                }

            }
        }
    }









}
